package android.homeaccount.helper;

import java.util.List;

import android.content.Context;

public class DataGetter {
	
	Context ct;
	DataHelper dh = null;
	public DataGetter(Context context)
	{
		this.ct = context;				
	}
	
	public Boolean CommitData(String user, String token)
	{
		dh = new DataHelper(ct);
		try{
			dh.deleteAll();
			dh.insert(user);
			dh.insert(token);
		}
		catch(Exception e)
		{		
			dh.close();
			return false;
		}
		dh.close();
		return true;
	}
	
	public String GetUser()
	{
		dh = new DataHelper(ct);
		List<String> names = dh.selectAll();
		if(names == null) return "";
		else if(!names.isEmpty())
		{
			try
			{
				String tmp = names.get(1);
				dh.close();
				return 	tmp;
			}
			catch(Exception e)
			{
				dh.close();
				return "";
			}
		}
		else
		{
			dh.close();
			return "";			
		}
	}
	
	public String GetToken()
	{
		dh = new DataHelper(ct);
		List<String> names = dh.selectAll();
		if(names == null) return "";
		else if(!names.isEmpty())
		{
			try
			{
				String tmp = names.get(0);
				dh.close();
				return tmp;
			}
			catch(Exception e)
			{
				dh.close();
				return "";
			}
		}
		else
		{
			dh.close();
			return "";
		}
	}
	
	public Boolean DeleteAll()
	{
		dh = new DataHelper(ct);
		try{
			dh.deleteAll();
			dh.close();
		}
		catch(Exception e)
		{			
			dh.close();
			return false;
		}
		return true;
	}
}
